<template>
  <div class="app">
    <el-card style="width: 480px">
      <template #header>
        <div class="card-header">
          <h1>登录</h1>
        </div>
      </template>

      <!-- 登录表单 -->

      <!-- :rules="rules" 整个表单的校验规则 -->
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <!-- prop="userName" --采用的 userName属性的校验规则-->
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="ruleForm.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"> 登录 </el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>

      <template #footer>Footer content</template>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { login } from '@/api/user.ts'
import { ElMessage } from 'element-plus'

let router = useRouter()

// 接口--RuleForm 控制表单中有哪些属性
interface RuleForm {
  userName: string
  password: string
}

const formSize = ref<ComponentSize>('default')

// 组件实例
const ruleFormRef = ref<FormInstance>()
// 响应式数据
const ruleForm = reactive<RuleForm>({
  userName: '',
  password: '',
})

//个表单的校验规则
// FormRules限制是的验证规则可以有哪些属性
const rules = reactive<FormRules<RuleForm>>({
  // userName属性的校验规则

  password: [
    {
      pattern: /^.{5,}$/,
      message: '密码至少5位非空字符',
      required: true,
    },
  ],
  userName: [
    //  required: true, message: '请输入用户名', trigger: 'blur'
    // 是否必须           失败提示                  触发条件
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 15, message: '用户名至少3位非空字符', trigger: 'blur' },
  ],
})

// formEl -form表单的组件实例
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return

  //validate对整个表单的内容进行验证。 接收一个回调函数，或返回 Promise。
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      //校验通过 -》调用登录接口--》登录成功进行跳转
      let r = await login(ruleForm.userName, ruleForm.password)
      console.log(r.data)
      if (r.data.code == 1) {
        ElMessage({
          message: '登陆成功',
          type: 'success',
          plain: true,
        })
        let token = localStorage.setItem('token', r.data.data)
        router.push('/home')
      } else {
        ElMessage({
          message: '登陆失败',
          type: 'error',
          plain: true,
        })
      }
    } else {
      // fields --校验不通过 规则对象
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>

<style scoped lang="scss">
.app {
  background-image: url('../assets/images/bg.jpg');
  height: 100%;
  display: flex;
  .el-card {
    margin: auto;
  }
}
</style>
